//
//  AppDelegate.m
//  SinaWeiBo
//
//  Created by apple on 16/5/27.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AppDelegate.h"
#include "QSMainTabBarController.h"
#import <IQKeyboardManager.h>
#import "QSNewFeatureViewController.h"
#import "QSWelcomeViewController.h"
#import "QSUserAccountViewModel.h"

@interface AppDelegate ()
/**
 *  用户第一次登录
 */
@property (nonatomic, assign) BOOL firstTime;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 设置程序窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 设置主窗口并显示
    [self.window makeKeyAndVisible];
    
    // 注册通知
    [self registerNotification];
    
    // 设置主题颜色
    [self setThemeColor];
    
    // 设置根视图控制器
    self.window.rootViewController = [self defaultRootController];
    
    // 设置键盘的自动显示和隐藏
    // 1.创建键盘管理器
    IQKeyboardManager *keyBoardManager = [IQKeyboardManager sharedManager];
    keyBoardManager.enable = YES;
    keyBoardManager.shouldResignOnTouchOutside = YES;
    keyBoardManager.shouldToolbarUsesTextFieldTintColor = YES;
    keyBoardManager.enableAutoToolbar = NO;
    
    return YES;
}

/**
 *  设置根视图控制器
 *
 *  @return 返回的控制器
 */
- (UIViewController *)defaultRootController
{
    // 判断用户是否登陆并且是否是更新版本
    if ([[[QSUserAccountViewModel alloc] init] isUserLongin]) {
        // 检测是否有新版本
        if ([self isNewVersion]) {
            return [[QSNewFeatureViewController alloc] init];
        }else{
            return [[QSWelcomeViewController alloc] init];
        }
    }
    return [[QSMainTabBarController alloc] init];
}

/**
 *  检测是否有新版本
 */
- (BOOL)isNewVersion
{
    // 获取新版本
    NSString *newVersionStr = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    double newVersionNum = newVersionStr.doubleValue;
    
    // 获取旧版本（一般保存在沙盒中）
    NSString *key = @"sandBoxKey";
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    double lastVersionNum = [userDefault doubleForKey:key];
    
    // 保存新版本
    [userDefault setDouble:newVersionNum forKey:key];
    
    // 同步沙盒
    [userDefault synchronize];
    
    return newVersionNum > lastVersionNum;
}

/**
 *  设置主题颜色
 */
- (void)setThemeColor
{
    // 设置UITabBar的主题
    [UITabBar appearance].tintColor = [UIColor orangeColor];
    
    // 设置UINavigationBar的主题
    [[UINavigationBar appearance] setTintColor:[UIColor orangeColor]];
}

/**
 *  注册通知
 */
- (void)registerNotification
{
    // 观测某个对象发出的通知，如果为nil就是观测所有的对象发出的通知
    NSNotificationCenter *defaultCen = [NSNotificationCenter defaultCenter];
    [defaultCen addObserver:self selector:@selector(switchToRootVC:) name:AppSwitchRootViewControler object:nil];
}

/**
 *  监听通知方法
 */
- (void)switchToRootVC:(NSNotification *)not
{
    if (not.object != nil) {
        self.window.rootViewController = [[QSNewFeatureViewController alloc] init];
    }
    self.window.rootViewController = [[QSMainTabBarController alloc] init];
}



/**
 *  移除通知
 */
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
